var Creator = function(){			
	
	var local_data = {};
	var items = new Array();
	var firstTime = false;
	var isAutomatic = false;
	
	this.setLocalData = function(data){
		local_data = data;
	};
	
	this.setFirstTime = function(data){
		firstTime = data;
	};
	
	this.setIsAutomatic = function(automatic){
		isAutomatic = automatic;
	};
	
	this.createInstragramPhotos = function createInstragramPhotos(){
		
		var result="";
		var numAleatorio = getRandomPic();
		var cont = 0;
		var totalItems = "";
		
		if(firstTime){
			$(".photosContainer").isotope("insert",getBlankDiv());
			$(".photosContainer").isotope("insert",getBlankDiv());	
		}
		
		$.each(local_data,function(key,val){
			
			if($("#"+val.id).length > 0 && isAutomatic){
				onLoadingItemsComplete(totalItems);
				return false;
			}else{
				result = getInstragramDiv();
				
				//for colorbox
				result = result.replace("#sourceBig#",val.images.low_resolution.url);
				if(val.caption != null){
					result = result.replace("#mensaje#",val.caption.text.substring(0, 50) + "&nbsp;|&nbsp; via&nbsp;");
				}else{
					result = result.replace("#mensaje#","via&nbsp;");	
				}
				
				result = result.replace("#link#",val.link);
				result = result.replace("#via#","@"+val.user.username);
				result = result.replace("#idInstagram#",val.id);
				
				if(isAutomatic){
					result = result.replace("#fecha#",getLatestDate());
				}else{
					result = result.replace("#fecha#",new Date().getTime());	
				}
											
				if(key == numAleatorio){
					result = result.replace("#source#",val.images.low_resolution.url);
					
					result = result.replace("#width#","460");
					result = result.replace("#height#","460");
					
					result = result.replace("#clase#","bigGeneral");
				}else{
					result = result.replace("#source#",val.images.low_resolution.url);
					
					result = result.replace("#width#","230");
					result = result.replace("#height#","230");
					
					result = result.replace("#clase#","smallGeneral");
				}
				result = result.replace("#username#",val.user.username);
				
				totalItems += result;			
				result = $(result);
				result.imagesLoaded(function(){
					cont++
					if(cont == local_data.length){
						cont = 0;
						onLoadingItemsComplete(totalItems);
					}							
				});	
				
			}
			
		});
		
		$(".ligthBoxGroup").colorbox({rel:'ligthBoxGroup'});
		
	}
	
	function getLatestDate(){
		var fechas = [];
		$.each($(".instragramPicGeneral"),function(key,val){
			
			if($(val).attr("fecha") != undefined){
				fechas.push($(val).attr("fecha"));
			}			
						
		});
		
		var biggest = Math.min.apply( null, fechas );
		
		biggest-=1;
		
		return biggest;
	};
	
	function onLoadingItemsComplete(totalItems){
		$(".photosContainer").isotope("insert",$(totalItems));
		$(".photosContainer").isotope( 'reloadItems' ).isotope({ sortBy: 'fecha' });		
	}
	
	function getInstragramDiv(){
		
		var instragramDiv = "<div id='#idInstagram#' fecha='#fecha#' class='instragramPicGeneral #clase#'>" +
							"<a class='ligthBoxGroup instagramPicContainer' href='#sourceBig#' data='#mensaje#' via='#via#' link='#link#' >" +
								"<img width='#width#' height='#height#' src='#source#'/>" +
							"</a>"+
							"<div class='viaUsername'> <p>VIA @#username#</p> </div>"+
						"</div>";
						
		return instragramDiv;
		
	}
	
	function getBlankDiv(){
		var result = "<div class='instragramPicGeneral smallGeneral'></div>";
		return $(result);
	}
	
	function getRandomPic(){
		var length = local_data.length;
		return Math.floor( (Math.random()*(length-1))+1)
	}
	
};